/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.cms.ejb;

import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.fyx.cms.controll.CmsArticleFacadeLocal;
import org.fyx.cms.entity.CmsArticle;
import org.fyx.core.PermissionDeniedException;
import org.fyx.core.context.FyxContextFactory;
import org.fyx.mula.ejb.MulaLocal;

/**
 *
 * @author ydenek
 */
@Stateless
public class CmsBean implements CmsLocal {

    public static final String ARTICLE_PREFIX = "cms_article_";
    @EJB
    private CmsArticleFacadeLocal cmsArticleFacade;
    @EJB
    private MulaLocal mula;
    
    @Override
    public void addArticle(CmsArticle article) {
        if (!FyxContextFactory.getContext().isUserInRole(CmsRoles.ARTICLES_MANAGER)) {
            throw new PermissionDeniedException();
        }
        article.setAuthor(FyxContextFactory.getContext().getUser());
        article.setTimeCreate(new Date());
        article.setTimeEditation(null);
        cmsArticleFacade.create(article);
        mula.setTranslate(ARTICLE_PREFIX + Integer.toString(article.getId()), article.getTitle(),
                article.getLanguage().getIsoLanguage());
    }

    @Override
    public void deleteArticle(CmsArticle ca) {
        cmsArticleFacade.remove(ca);
        mula.removeTranslate(ARTICLE_PREFIX + Integer.toString(ca.getId()),
                FyxContextFactory.getContext().getIsoLanguage());

    }

    @Override
    public List<CmsArticle> getArticles() {
        return cmsArticleFacade.findAll();
    }

    @Override
    public CmsArticle getArticleById(int idArticle) {
        return cmsArticleFacade.find(idArticle);
    }

    @Override
    public void editArticle(CmsArticle ca) {
        cmsArticleFacade.edit(ca);
    }
}
